
#include "xapp_config.h"

#include "pbm.h"

#include "xapp_access.h"

#if USER_CHECK_ENABLED( ALARMCLOCK )
#include "xapp_alarmclock.h"
#endif 

#if USER_CHECK_ENABLED( PHYSIC_PERIOD )
#include "xapp_phys_period.h"
#endif

#if 1 // Application 
pbd_err_code_t pi_app_write_alarm_clock(uint32_t idx, uint8_t *pdata, uint16_t size)
{
    #if USER_CHECK_ENABLED( ALARMCLOCK )
    if(xapp_alarmclock_write(idx, pdata, size) > 0)
    {
        return PBD_ERR_INVALID_FORMAT; 
    }
    return PBD_SUCCESS; 
    #else 
    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
    #endif 
}
pbd_err_code_t pi_app_read_alarm_clock(uint32_t idx, uint8_t *pdata, uint16_t *p_size)
{    
    #if USER_CHECK_ENABLED( ALARMCLOCK )
    if( xapp_alarmclock_read(idx, pdata, p_size) > 0)
    {
        return PBD_ERR_NO_MEM; 
    }
    return PBD_SUCCESS;
    #else 
    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
    #endif 
}

pbd_err_code_t pi_app_write_to_do_tip(uint32_t idx, uint8_t *pdata, uint16_t size)
{
    return PBD_SUCCESS;
}
pbd_err_code_t pi_app_write_schedule(uint32_t idx, uint8_t *pdata, uint16_t size)
{
    return PBD_SUCCESS; 
}

pbd_err_code_t pi_app_read_to_to_tip(uint32_t idx, uint8_t *pdata, uint16_t size)
{
    return PBD_SUCCESS;
}

pbd_err_code_t pi_app_read_schedule(uint32_t idx, uint8_t *pdata, uint16_t size)
{
    return PBD_SUCCESS; 
}

pbd_err_code_t pi_app_read_physic_period(uint8_t* response, uint16_t* maxlen)
{
    #if USER_CHECK_ENABLED( PHYSIC_PERIOD )
    xapp_phys_period_info_t const *p_phsc_period_info = xapp_phys_period_get_info();
    response[0] = p_phsc_period_info->type;
    response[1] = p_phsc_period_info->year;
    response[2] = p_phsc_period_info->month;
    response[3] = p_phsc_period_info->day;
    response[4] = p_phsc_period_info->cycle;
    *maxlen = 5; 
    #else 
    *maxlen = 0;
    #endif

    return PBD_SUCCESS;
}
pbd_err_code_t pi_app_write_physic_period(uint8_t* pdata, uint16_t size )
{
    #if USER_CHECK_ENABLED( PHYSIC_PERIOD )
    if( size != 5 )
    {
        return PBD_ERR_INVALID_LENGTH;
    }

    uint8_t type = pdata[0];
    uint8_t year = pdata[1];
    uint8_t month = pdata[2];
    uint8_t day = pdata[3];
    uint8_t actual_days = pdata[4];

    if( !xapp_physc_period_set_info( type, year, month, day, actual_days) )
    {
        return PBD_ERR_INVALID_PARAMS; 
    }

    return PBD_SUCCESS; 
    #else 
    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
    #endif    
}

#endif // ! application 

